'use strict';

import Base from './base.js';

export default class extends Base {

	async messagelistAction(){
		let messageTemplate = await this.model('message_classify').select();

		this.assign('mess', JSON.stringify(messageTemplate));

		return this.display();
	}

	async getmessagetemplateAction(){
		let page = this.post();
		let id = page.id;
		let pages = page.pages;
		let limits = page.limits;

		let msgList = await this.model('message_demo').where({pid: id}).page(pages, limits).countSelect();

		return this.json(msgList);
	}

	async gettmplistAction(){
		let list = await this.model('message_classify').select();
		return this.json(list);
	}

	async addtmpsAction(){
		let data = this.post();
		let res = await this.model('message_classify').add(data);
		return res?this.success('添加成功'):this.fail('添加失败');
	}

	// 删除短信模版内容
	async delcontentAction(){
		let id = this.post('id');
		let res = await this.model('message_demo').where({id: id}).delete();
		return res?this.success('删除成功'):this.fail('删除失败');
	}

	// 修改短信模版内容
	async editcontentAction(){
		let row = this.post();
		let addtime = Math.round(new Date().valueOf()/1000);
		row.addtime = addtime;
		let res = await this.model('message_demo').update(row);
		return res?this.success('修改成功'):this.fail('修改失败');
	}

	// 添加短信模版内容
	async addcontentAction(){
		let msg = this.post();
		if (!msg.pid) {
			return this.fail('添加失败，请重新添加');
		}
		msg.addtime = Math.round(new Date().valueOf()/1000);

		let res = await this.model('message_demo').add(msg);

		return res?this.success('添加成功'):this.fail('添加失败');
	}


}